<template>
  <button class="go-button-loading" :class="{'onclic':active,'validate':validated}" @click="toLoadButton">
    {{ validated ? afterText : active ? '' : beforeText }}
  </button>
</template>

<script>
import {mapMutations, mapState} from "vuex";

export default {
  name: "GoButtonLoading",
  props: {
    beforeText: {
      type: String,
      default: 'SUBMIT'
    },
    afterText: {
      type: String,
      default: 'SUCCESS'
    },
    message: {
      type: String,
      default: 'Secondary confirmation'
    },
    successFunction: {
      type: Function
    },
    validate: {
      type: Boolean,
      default: true
    },
    errorText: {
      type: String,
      default: '出现了一些小错误'
    },
    height: {
      type: Number,
      default: 90
    },
    font: {
      type: Number,
      default: 17
    }
  },
  data() {
    return {
      active: false,
      validated: false
    }
  },
  methods: {
    ...mapMutations(['notify']),
    toLoadButton() {
      if (!this.validate) {
        this.notify({msg: this.errorText, duration: 2000, font: this.font, height: this.height + "px"})
        setTimeout(() => {
          this.active = true;
        }, 100)
        setTimeout(() => {
          this.active = false;
        }, 1000)
        return;
      }

      if (this.validated || this.active) return;

      setTimeout(() => {
        this.active = true;
      }, 100)


      this.notify({
        msg: this.message, action: true, font: this.font, height: this.height + "px", success: () => {
          if (this.notifySuccess) {
            this.successFunction();
            this.active = false;
            this.validated = true
            setTimeout(() => {
              this.validated = false;
            }, 2000)
          } else {
            this.active = false;
          }
        }
      });
    }
  },
  computed: {
    ...mapState(['notifySuccess'])
  }
}

</script>

<style lang="less">

.go-button-loading {
  outline: none;
  height: 40px;
  margin: auto;
  text-align: center;
  width: 100px;
  border-radius: 40px;
  background: #fff;
  border: 2px solid #1ECD97;
  color: #1ECD97;
  letter-spacing: 1px;
  font-size: 12px;
  font-weight: bold;
  cursor: pointer;
  transition: all 0.25s ease;
}

.go-button-loading:active {
  color: white;
  background: #1ECD97;
}

button:active {
  letter-spacing: 2px;
}

.onclic {
  width: 40px;
  border-color: #bbbbbb;
  border-width: 3px;
  font-size: 0;
  border-left-color: #1ECD97;
  -webkit-animation: rotating 2s 0.25s linear infinite;
  animation: rotating 2s 0.25s linear infinite;
}

.onclic:after {
  content: "";
}

.onclic:hover {
  color: #1ECD97;
  background: white;
}

.validate {
  font-size: 13px;
  color: white;
  background: #1ECD97;
}

@-webkit-keyframes rotating {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

@keyframes rotating {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}
</style>